using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace OrtzEng.Text
{
    public class TextManager
    {

        public TextManager(GraphicsDevice graphics, ContentManager loader)
        {
            FontTexture = loader.Load<Texture2D>("./resources/images/font") as Texture2D;
        }//End New

        Dictionary<string, Text> mTexts = new Dictionary<string, Text>();
        public Dictionary<string, Text> Texts
        {
            get { return mTexts; }
            set { mTexts = value; }
        }

        Texture2D mFontTexture;
        public Texture2D FontTexture
        {
            get { return mFontTexture; }
            set { mFontTexture = value; }
        }

        public void Draw(SpriteBatch theSpriteBatch)
        {

            foreach (KeyValuePair<string, Text> aText in Texts)
            {
                if (aText.Value.Visible == true)
                {
                    char[] aCharacters = aText.Value.Value.ToCharArray();
                    int aXPositionOffset = (int)aText.Value.Position.X;
                    foreach (char aChar in aCharacters)
                    {
                        Rectangle bitmapPosition = GetLetterValue(aChar);
                        theSpriteBatch.Draw(FontTexture, new Rectangle(aXPositionOffset, (int)aText.Value.Position.Y, aText.Value.Size, aText.Value.Size), bitmapPosition, Color.White);
                        aXPositionOffset += aText.Value.Size;
                    }
                }
            }
        }

        public void Draw(SpriteBatch theSpriteBatch, string theKey)
        {
            char[] aCharacters = Texts[theKey].Value.ToCharArray();
            int aXPositionOffset = (int)Texts[theKey].Position.X;
            foreach (char aChar in aCharacters)
            {
                theSpriteBatch.Draw(FontTexture, new Rectangle(aXPositionOffset, (int)Texts[theKey].Position.Y, Texts[theKey].Size, Texts[theKey].Size), GetLetterValue(aChar), Color.White);
                aXPositionOffset += Texts[theKey].Size;
            }
        }

        protected Rectangle GetLetterValue(char theCharacter)
        {
            Vector2 aNumberPosition = new Vector2(0, 0);
            switch (theCharacter)
            {
                case 'A':
                    aNumberPosition = new Vector2(3, 2);
                    break;
                case 'B':
                    aNumberPosition = new Vector2(4, 2);
                    break;
                case 'C':
                    aNumberPosition = new Vector2(5, 2);
                    break;
                case 'D':
                    aNumberPosition = new Vector2(6, 2);
                    break;
                case 'E':
                    aNumberPosition = new Vector2(7, 2);
                    break;
                case 'F':
                    aNumberPosition = new Vector2(8, 2);
                    break;
                case 'G':
                    aNumberPosition = new Vector2(9, 2);
                    break;
                case 'H':
                    aNumberPosition = new Vector2(10, 2);
                    break;
                case 'I':
                    aNumberPosition = new Vector2(11, 2);
                    break;
                case 'J':
                    aNumberPosition = new Vector2(12, 2);
                    break;
                case 'K':
                    aNumberPosition = new Vector2(13, 2);
                    break;
                case 'L':
                    aNumberPosition = new Vector2(14, 2);
                    break;
                case 'M':
                    aNumberPosition = new Vector2(15, 2);
                    break;
                case 'N':
                    aNumberPosition = new Vector2(0, 3);
                    break;
                case 'O':
                    aNumberPosition = new Vector2(1, 3);
                    break;
                case 'P':
                    aNumberPosition = new Vector2(2, 3);
                    break;
                case 'Q':
                    aNumberPosition = new Vector2(3, 3);
                    break;
                case 'R':
                    aNumberPosition = new Vector2(4, 3);
                    break;
                case 'S':
                    aNumberPosition = new Vector2(5, 3);
                    break;
                case 'T':
                    aNumberPosition = new Vector2(6, 3);
                    break;
                case 'U':
                    aNumberPosition = new Vector2(7, 3);
                    break;
                case 'V':
                    aNumberPosition = new Vector2(8, 3);
                    break;
                case 'W':
                    aNumberPosition = new Vector2(9, 3);
                    break;
                case 'X':
                    aNumberPosition = new Vector2(10, 3);
                    break;
                case 'Y':
                    aNumberPosition = new Vector2(11, 3);
                    break;
                case 'Z':
                    aNumberPosition = new Vector2(12, 3);
                    break;
                case '0':
                    aNumberPosition = new Vector2(2, 1);
                    break;
                case '1':
                    aNumberPosition = new Vector2(3, 1);
                    break;
                case '2':
                    aNumberPosition = new Vector2(4, 1);
                    break;
                case '3':
                    aNumberPosition = new Vector2(5, 1);
                    break;
                case '4':
                    aNumberPosition = new Vector2(6, 1);
                    break;
                case '5':
                    aNumberPosition = new Vector2(7, 1);
                    break;
                case '6':
                    aNumberPosition = new Vector2(8, 1);
                    break;
                case '7':
                    aNumberPosition = new Vector2(9, 1);
                    break;
                case '8':
                    aNumberPosition = new Vector2(10, 1);
                    break;
                case '9':
                    aNumberPosition = new Vector2(11, 1);
                    break;
                case '+':
                    aNumberPosition = new Vector2(13, 0);
                    break;
                case '-':
                    aNumberPosition = new Vector2(15, 0);
                    break;
                case '/':
                    aNumberPosition = new Vector2(1, 1);
                    break;
                case '*':
                    aNumberPosition = new Vector2(12, 0);
                    break;
                case ':':
                    aNumberPosition = new Vector2(12, 1);
                    break;
                case ' ':
                    aNumberPosition = new Vector2(2, 0);
                    break;
            }

            return new Rectangle((int)aNumberPosition.X * 8, (int)aNumberPosition.Y * 8, 8, 8);
        }


        public void Update(float elapsed)
        {

            foreach (KeyValuePair<string, Text> aText in Texts)
            {
                if (aText.Value.VisibleTime != 0)
                {
                    aText.Value.Elapsed -= elapsed;
                }
            }

        }

    }
}
